package org.ouwh.fw.model;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ouwh.fw.model.data.Data;
import org.ouwh.fw.model.data.DataLoader;
import org.ouwh.fw.model.module.Module;
import org.ouwh.fw.model.module.ModuleLoader;

public class ModelReader {
	private static Log log = LogFactory.getLog(ModelReader.class);

	private DataLoader dataLoader;

	private ModuleLoader moduleLoader;

	public ModelReader(Class<?> clazz) {
		try {
			dataLoader = new DataLoader(clazz);
			moduleLoader = new ModuleLoader(clazz);
		} catch (Exception e) {
			log.error("ModelReader init error: " + clazz.getName(), e);
		}
	}

	public Data readData(String name) throws Exception {
		if (dataLoader == null) {
			throw new Exception("DataLoader not initialized.");
		}
		return (Data) dataLoader.loadObjectByName(name);
	}

	public Module readModule(String name) throws Exception {
		if (moduleLoader == null) {
			throw new Exception("ModuleLoader not initialized.");
		}
		return (Module) moduleLoader.loadObjectByName(name);
	}

}
